# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     crawler_task
   Description :
   Author :       wuqianyong
   date：          2020/7/13
-------------------------------------------------
   Change Activity:
                   2020/7/13:
-------------------------------------------------
"""
__author__ = 'wuqianyong'

from pandas import read_excel,DataFrame
import time, datetime
import json
from config import headless,driver_path

from extra_apps.common_log import Log
from selenium.webdriver import Chrome, ChromeOptions, ActionChains

class CrawlerTask(object):

    def __init__(self):
        self.load_chrome()
        self.start_url = 'aaa'
        self.batch = str(datetime.datetime.now())[:16].replace(":","")
        self.item_list = list()

    def get_company_list(self, sheet_name):
        df = read_excel("Input.xlsx", sheet_name=sheet_name)
        company_list = [x.get("{}_COMPANY_NAME".format(sheet_name)) for x in df.to_dict(orient="record")]
        return company_list

    def load_chrome(self):
        chrome_option = ChromeOptions()
        # headless =
        user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
        if headless:
            chrome_option.add_argument('--headless')  # 无界面模式
        chrome_option.add_argument('--no-sandbox')
        chrome_option.add_argument('--disable-gpu')
        chrome_option.add_argument('User-Agent="{}"'.format(user_agent))
        Log.info("dirver path is :{}".format(driver_path))
        self.chrome = Chrome(executable_path=driver_path, options=chrome_option)
        self.chrome.maximize_window()

    def save_to_excel(self):
        df = DataFrame(self.item_list)
        fn = "~/Desktop/{}-{}.xlsx".format(self.name,self.batch)
        Log.info("save to ---> {}".format(fn))
        df.to_excel(fn,index=False)
